/**
 * @Date 2025/1/5 14:57
 * @Created by 衡盛永
 */

package org.example.deploy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.deploy.common.MyUserDetails;
import org.example.deploy.mapper.SysPermissionMapper;
import org.example.deploy.mapper.SysRoleMapper;
import org.example.deploy.mapper.SysUserMapper;
import org.example.deploy.pojo.SysPermission;
import org.example.deploy.pojo.SysRole;
import org.example.deploy.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service("userDetailsService")
public class UserDetailsServiceImpl
        implements UserDetailsService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    // 自定义认证逻辑
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 1.构造查询条件
        QueryWrapper<SysUser> wrapper = new QueryWrapper<SysUser>().eq("username", username);
        // 2.查询用户
        SysUser user = sysUserMapper.selectOne(wrapper);
        if (user == null) {
            throw new UsernameNotFoundException(username); // 用户名不存在异常
        }
        user.setPassword(user.getPassword());
        // 通过用户id获取角色id
        Integer roleIdByUserId = sysRoleMapper.getRoleIdByUserId(user.getId());
        // 从数据库中查询的当前用户的所有权限
        ArrayList<String> auths = new ArrayList<>();
        List<Integer> permissionIdsByRoleId = sysPermissionMapper.getPermissionIdsByRoleId(roleIdByUserId);
        List<SysPermission> sysPermissions;
        // 如果没有找到权限 ID，返回空列表
        if (permissionIdsByRoleId == null || permissionIdsByRoleId.isEmpty()) {
            sysPermissions = Collections.emptyList();  // 返回一个空的列表，避免 null
        } else {
            sysPermissions = sysPermissionMapper.selectBatchIds(permissionIdsByRoleId);
        }
        for (SysPermission sysPermission : sysPermissions) {
            auths.add(sysPermission.getPermissionRemark());
        }
        // 从数据库中查询的当前用户的所有角色
        SysRole sysRole = sysRoleMapper.selectOne(new QueryWrapper<SysRole>().eq("id", roleIdByUserId));
        ArrayList<String> roles = new ArrayList<>();
        roles.add("ROLE_" + sysRole.getRoleRemark());
        // 当前用户的角色和权限 合并权限、角色到 authoritieStrList 中
        List<String> authoritieStrList = Stream.concat(auths.stream(), roles.stream())
                .collect(Collectors.toList());
        // 返回封装好的UserDetails对象
        return new MyUserDetails(user, authoritieStrList);
    }
}